package light.head.dao.site;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import light.head.constant.Parm;
import light.head.enm.RESULT;
import light.head.framework.base.BaseDAO;
import light.head.framework.entity.PageBean;
import light.head.model.Certificates;
import light.head.model.CertificatesAnnex;
import light.head.model.FileInfo;
import org.springframework.stereotype.Service;
/**
 * 荣誉证书
 */
@Service
public class CertificatesDAO extends BaseDAO<Certificates, Serializable> {
	private static final String sql_findAnnex = "FROM CertificatesAnnex ca WHERE ca.cc = ?";
	private static final String sql_indexlist = "FROM Certificates c WHERE c.deleted = 0 AND c.oc = ?";
	public RESULT findByMap(Map<String, String> map, PageBean pageBean) {
		StringBuffer hql = new StringBuffer("FROM Certificates c WHERE 1 = 1");
		List<Object> list = null;
		if (map != null) {
			list = new ArrayList<Object>();
			String name = map.get(Parm.NAME);
			String deleted = map.get(Parm.DELETED);
			String oc = map.get(Parm.OC);
			if (oc != "" && isNotNull(oc)) {
				hql.append(" AND c.oc = ? ");
				list.add(Integer.parseInt(oc));
			}
			if (name != "" && isNotNull(name)) {
				hql.append(" AND c.name LIKE ? ");
				list.add("%" + name + "%");
			}
			if (deleted != "" && isNotNull(deleted)) {
				hql.append(" AND c.deleted = ?");
				list.add(Boolean.parseBoolean(deleted));
			}
		}
		hql.append(" ORDER BY c.code");

		return findToPageBean(hql.toString(), pageBean, list);
	}

	public List<FileInfo> findFileByCodes(String codes) {
		if (!isNotNull(codes)) {
			return null;
		}
		String sql = "FROM FileInfo f WHERE f.code IN (" + codes + ")";
		List<FileInfo> list = find(sql);
		return list;
	}

	public List<CertificatesAnnex> findAnnexByCode(Integer code) {
		return super.find(sql_findAnnex, code);
	}

	public RESULT deleteAnnexNotInCodes(Integer code, List<Object> codes) {
		StringBuffer sql = new StringBuffer();
		List<Object> parm = new ArrayList<Object>();
		parm.add(code);
		for (Object o : codes) {
			if(o != null){
				sql.append("?,");
				parm.add(o);
			}
		}
		if (sql.length() > 0) {
			sql.insert(0, " AND ca.code NOT IN(")
					.deleteCharAt(sql.length() - 1).append(")");
		}
		sql.insert(0, "DELETE FROM CertificatesAnnex ca WHERE ca.cc = ?");
		execute(sql.toString(), parm.toArray());
		return RESULT.SUCCESS;
	}
	//获取首页List
	public List<Certificates> getIndexList(Integer code){
		return super.find(sql_indexlist, code);
	}
}
